﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace TimViec.NTD
{
    public partial class NTDUpdate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["NTD"] == null)
            {
                Response.Redirect("/nha-tuyen-dung.html");
            }
            CKSoLuocCty.Language = "vi";
            CKFinder.FileBrowser _FileBrowser = new CKFinder.FileBrowser();
            _FileBrowser.BasePath = "/ckfinder/";
            _FileBrowser.SetupCKEditor(CKSoLuocCty);     
            if(!IsPostBack)
            {
                LoadData();
                LoadThongTin();
            }
        }
        public  void LoadThongTin()
        {
            DAL.UserTuyenDung userTuyenDung = BLL.TuyenDung.UserHelper.Get_UserEmail(Session["NTD"].ToString());
            if (userTuyenDung !=null)
            {
                lbTenTaiKhoan.Text = userTuyenDung.Email;
                txtDiaChi.Text = userTuyenDung.DiaChi;
                txtDienThoai.Text = userTuyenDung.DienThoai;
                txtDienThoaiLienHe.Text = userTuyenDung.DienThoaiLienHe;
                txtEmailLienHe.Text = userTuyenDung.EmailLienHe;
                txtFax.Text = userTuyenDung.Fax;
                txtNguoiLienHe.Text = userTuyenDung.NguoiLienHe;
                txtSoNhanVien.Text = userTuyenDung.SoNhanVien;
                txtTenCongTy.Text = userTuyenDung.TenCongTy;
                txtTenLienHe.Text = userTuyenDung.TenLienHe;
                txtWebsite.Text = userTuyenDung.Website;
                CKSoLuocCty.Text = userTuyenDung.SoLuocCongTy;
                //
                droTinh.SelectedValue = userTuyenDung.TinhId.ToString();
            }
        }
        public  void LoadData()
        {
            try
            {
                DAL.UserTuyenDung userTuyenDung = BLL.TuyenDung.UserHelper.Get_UserEmail(Session["NTD"].ToString());
                droQuocGia.DataSource = BLL.Admin.QuocGiaHelper.List_QuocGia();
                droQuocGia.DataTextField = "Ten";
                droQuocGia.DataValueField = "QuocGiaId";
                droQuocGia.DataBind();

                droQuocGia.SelectedValue = BLL.Admin.TinhHelper.Get_TinhId(int.Parse(userTuyenDung.TinhId.ToString())).QuocGiaId.ToString();
                //
                droTinh.DataSource = BLL.Admin.TinhHelper.List_Tinh_Theo_QuocGia(int.Parse(droQuocGia.SelectedValue));
                droTinh.DataTextField = "Ten";
                droTinh.DataValueField = "TinhId";
                droTinh.DataBind();
            }
            catch
            {
            }
        }
        protected void droQuocGia_SelectedIndexChanged(object sender, EventArgs e)
        {
            droTinh.DataSource = BLL.Admin.TinhHelper.List_Tinh_Theo_QuocGia(int.Parse(droQuocGia.SelectedValue));
            droTinh.DataTextField = "Ten";
            droTinh.DataValueField = "TinhId";
            droTinh.DataBind();
        }
        private string UploadFile(FileUpload File)
        {
            //string Today = DateTime.Now.Year.ToString() + "/" + DateTime.Now.Month.ToString() + "/" + DateTime.Now.Day.ToString();
            string today = DateTime.Now.ToShortDateString();
            string UploadPath = "/UserFile/NTD/";
            string FilePath = UploadPath + DateTime.Now.ToShortDateString().Replace('/', '-') + DateTime.Now.ToLongTimeString().Replace(':', '-') + DateTime.Now.Millisecond + File.FileName;
            //string FilePath = UploadPath + File.FileName;

            /* N?u thu m?c t?n t?i */
            if (Directory.Exists(Server.MapPath(UploadPath)))
            {
                File.SaveAs(Server.MapPath(FilePath));
                //Utils.Help.CreateThumbnail(Server.MapPath(FilePath), 140, 105);
            }
            else
            {
                /* T?o thu m?c n?u chua t?n t?i */
                Directory.CreateDirectory(Server.MapPath(UploadPath));
                File.SaveAs(Server.MapPath(FilePath));
            }

            return FilePath;
        }
        protected void btCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("/nha-tuyen-dung/index.html");
        }

        protected void btSave_Click(object sender, EventArgs e)
        {
            DAL.UserTuyenDung userTuyenDung = BLL.TuyenDung.UserHelper.Get_UserEmail(Session["NTD"].ToString());
            string upfile = null;
            try
            {                
                if (FileUpLoad_Logo.HasFile)
                {
                    if (FileUpLoad_Logo.PostedFile.ContentLength > 1049000)
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('File ảnh phải nhỏ hơn 1Mb!');", true);
                        return;
                    }
                    upfile = UploadFile(FileUpLoad_Logo);
                }
                else
                {
                    upfile = null;
                }                
                BLL.TuyenDung.UserHelper.Update_UserTuyenDung(int.Parse(userTuyenDung.UserId.ToString()), txtTenCongTy.Text, CKSoLuocCty.Text, txtSoNhanVien.Text, txtWebsite.Text, upfile, txtTenLienHe.Text, txtDiaChi.Text, int.Parse(droTinh.SelectedValue), txtDienThoai.Text, txtFax.Text, txtEmailLienHe.Text, txtNguoiLienHe.Text, txtDienThoaiLienHe.Text);
                Response.Redirect("/nha-tuyen-dung/index.html");             
            }
            catch
            {

            }
        }
    }
}